<?php
if ($_POST) {
    include_once("../../Functions/connectDatabase.php");
    get_connection();
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["name"])) {
            echo "<script>alert('新闻类别的名称不能为空！');
            window.location.href = document.referrer;
        </script>";
        } else {
            $name = $_POST["name"];
            $sql = "SELECT * FROM category WHERE category_name='$name'";
            $result = mysqli_query($conn, $sql);
            if ($result->num_rows == 0) {
                $sql = "INSERT INTO category (category_name)
        VALUES ('$name')";
                if (mysqli_query($conn, $sql)) {
                    echo "<script>alert('添加成功！');window.location.href='./adminShow.php'</script>";
                } else {
                    $error = mysqli_error($conn);
                    $error = str_replace('\'', '', $error);
                    echo "<script>alert('添加失败！$error');window.location.href='./adminShow.php'</script>";
                }
            } else {
                echo "<script>alert('添加失败！已存在相同名称的新闻类型！');window.location.href='./adminShow.php'</script>";
            }
        }
    }
    close_connection();
}
?>